package servlet;

import Service.Impl.flowerServiceImpl;
import Service.flowerService;
import com.alibaba.fastjson.JSON;
import entity.Flower;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import static java.lang.Integer.parseInt;

@WebServlet("/a")
public class flowerservlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        flowerService obj = new flowerServiceImpl();
        List<Flower> list = obj.getAll();
        String flower = JSON.toJSONString(list);
        out.print(flower);

        String opr = req.getParameter("opr");
        if ("add".equals(opr)) {
            String name = req.getParameter("name");
            String anothername = req.getParameter("anothername");
            String property = req.getParameter("property");
            int price = parseInt(req.getParameter("price"));
            String production = req.getParameter("production");
            int addHs = obj.add(name, anothername, property, price, production);
            String sc = req.getContextPath();
            if (addHs > 0) {
                out.print("<script>alert('新增成功')</script>");
                resp.sendRedirect(sc + "/index.jsp");
            } else if ("delete".equals(opr)) {
                int did = parseInt(req.getParameter("id"));
                int deleths = obj.delete(did);
                String bc = req.getContextPath();
                if (deleths > 0) {
                    out.print("<script>alert('删除成功')</script>");
                    resp.sendRedirect(bc + "/index.jsp");
                }
            } else if ("update".equals(opr)) {
                int id2 = parseInt(req.getParameter("upid"));
                String name2 = req.getParameter("upname");
                String anothername2 = req.getParameter("anothername");
                String property2 = req.getParameter("property");
                int price2 = parseInt(req.getParameter("price"));
                String production2 = req.getParameter("production");
                int uphs = obj.update(id2, name2, anothername2, property2, price2, production2);
                String ac = req.getContextPath();
                if (uphs > 0) {
                    out.print("<script>alert('修改成功')</script>");
                    resp.sendRedirect(ac + "/index.jsp");
                }
            }
        }
    }
}
